package com.ruoyi.web.service;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TaskDataService {

    @Autowired
    private DruidDataSource druidDataSource;

    // Service 方法，获取任务数据，并返回 JSON 格式的 List
    public List<Map<String, Object>> getLatestTaskData() {
        List<Map<String, Object>> dataList = new ArrayList<>();
        String sql = "SELECT `周期`, `C2集成次数`, `C2成功次数` FROM `integrated_info`;";

        try (Connection conn = druidDataSource.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {

            while (rs.next()) {
                String period = rs.getString("周期");
                int integratedTimes = rs.getInt("C2集成次数");
                int successTimes = rs.getInt("C2成功次数");

                // 使用 Map 来构造返回的 JSON 数据
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("week", period);
                dataMap.put("integrationCount", integratedTimes);
                dataMap.put("successCount", successTimes);
                dataList.add(dataMap);

                System.out.println("周期:" + period + " C2集成次数:" + integratedTimes + " C2成功次数:" + successTimes);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return dataList;
    }
}
